from functools import lru_cache
import pandas as pd
import numpy as np
import json
import os
from typing import Dict, List, Any, Optional
from config import get_data_file_path

class DataProcessor:
    """数据处理和格式化工具类，专为数据可视化优化"""

    @staticmethod
    @lru_cache(maxsize=32)
    def load_json_data(filename: str) -> pd.DataFrame:
        """缓存的数据加载函数"""
        try:
            file_path = get_data_file_path(filename)
            with open(file_path, 'r', encoding='utf-8') as f:
                data = pd.read_json(f)
            return data
        except Exception as e:
            print(f"加载数据失败 {filename}: {e}")
            return pd.DataFrame()

    @staticmethod
    def clean_data_for_json(df: pd.DataFrame) -> List[Dict]:
        """清理数据并转换为JSON兼容格式"""
        # 处理NaN值
        df_clean = df.replace({np.nan: None, pd.NaT: None})

        # 转换numpy数据类型为Python原生类型
        for col in df_clean.select_dtypes(include=['float64', 'int64']).columns:
            df_clean[col] = df_clean[col].apply(
                lambda x: x.item() if pd.notnull(x) and hasattr(x, 'item') else x
            )

        # 处理时间戳
        for col in df_clean.select_dtypes(include=['datetime64']).columns:
            df_clean[col] = df_clean[col].dt.strftime('%Y-%m-%d %H:%M:%S')

        return df_clean.to_dict("records")

    @staticmethod
    def format_for_charts(data: List[Dict], x_field: str, y_fields: List[str]) -> Dict:
        """为图表组件优化数据格式"""
        return {
            "categories": [item[x_field] for item in data],
            "series": [
                {
                    "name": field,
                    "data": [item.get(field, 0) for item in data]
                } for field in y_fields
            ]
        }

    @staticmethod
    def format_for_table(data: List[Dict], columns: Optional[List[str]] = None) -> Dict:
        """为表格组件优化数据格式"""
        if not data:
            return {"columns": [], "rows": []}

        if columns is None:
            columns = list(data[0].keys()) if data else []

        return {
            "columns": columns,
            "rows": [
                [item.get(col, "") for col in columns]
                for item in data
            ]
        }

    @staticmethod
    def get_data_summary(df: pd.DataFrame) -> Dict:
        """获取数据概要信息"""
        return {
            "total_records": len(df),
            "columns": list(df.columns),
            "data_types": df.dtypes.astype(str).to_dict(),
            "missing_values": df.isnull().sum().to_dict(),
            "numeric_summary": df.describe().to_dict() if len(df.select_dtypes(include=[np.number]).columns) > 0 else {}
        }